﻿#ifndef __SS_UNIQUE_PTR_H__
#define __SS_UNIQUE_PTR_H__
#include "../ssBaseDefine.h"

namespace StarSeeker
{

//该容器保证实际指针只在最新的对象中保存，所有复制操作的旧对象的值都被清零，
//该容器线程不安全，只能用于单线程
template<typename T>
class ssUniquePtr
{
public:
	ssUniquePtr()
	{
		m_ptr = 0;
	}
	ssUniquePtr(T* p)
	{
		m_ptr = p;
	}
	ssUniquePtr(const ssUniquePtr& up)
	{
		m_ptr = up.Release();
	}
	void operator = (const ssUniquePtr& up)
	{
		Reset(up.Release());
		m_ptr = 0;
	}
	~ssUniquePtr()
	{
		SS_SAFE_DELETE(m_ptr)
	}
	T* operator -> ()
	{
		return m_ptr;
	}
	T& operator * ()
	{
		return *m_ptr;
	}
	T* Release() const
	{
		T* tmp = m_ptr;
		m_ptr = 0;
		return tmp;
	}
	void Reset(T* p = 0)
	{
		if (p != m_ptr)
		{
			SS_SAFE_DELETE(m_ptr)
		}
		m_ptr = p;
	}
	T* Get() const
	{
		return m_ptr;
	}

private:
	T*	m_ptr;
};

}
#endif
